package cn.tedu.boot031.controller;

import cn.tedu.boot031.entity.Emp;
import cn.tedu.boot031.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Controller
public class EmpController {
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        //获取数据库连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "insert into myemp values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setInt(2,emp.getAge());
            ps.setString(3,emp.getJob());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return "添加完成!<a href='/'>返回首页</a>";
    }
    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        ArrayList<Emp> list = new ArrayList<>();
        try (Connection conn = DBUtils.getConn()){
            String sql = "select id,name,age,job from myemp";
            PreparedStatement ps = conn.prepareStatement(sql);
            //执行查询
            ResultSet rs = ps.executeQuery();
            //遍历结果集对象
            while(rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                int age = rs.getInt(3);
                String job = rs.getString(4);
                Emp e = new Emp();
                e.setId(id);
                e.setName(name);
                e.setAge(age);
                e.setJob(job);
                list.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //把员工的数据装进表格标签里面
        String html = "<table border='1'>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>名字</th><th>年龄</th><th>工作</th><th>操作</th></tr>";
        //遍历list集合
        for (Emp e :list) {
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getAge()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        try (Connection conn = DBUtils.getConn()){
            String sql = "delete from myemp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除完成!<a href='/select'>返回列表页面</a>";
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(Emp emp){
        try (Connection conn = DBUtils.getConn()){
            String sql = "update myemp set name=?,age=?,job=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setInt(2,emp.getAge());
            ps.setString(3,emp.getJob());
            ps.setInt(4,emp.getId());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改完成!<a href='/select'>返回列表页面</a>";
    }
}
